#include "ShopCartWindow.h"
#include "ui_ShopCartWindow.h"
#include "ShopCartItem.h"
#include "ui_ShopCartItem.h"

int total_price = 0;    //总价
QString bill = "";   //账单

ShopCartWindow::ShopCartWindow(QWidget *parent) :
    QListWidget(parent),
    ui(new Ui::ShopCartWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("购物车");    //设置标题
    ui->price_label->setText(QString::number(total_price));
}

ShopCartWindow::~ShopCartWindow()
{
    delete ui;
}

//响应添加进购物车的槽函数
void ShopCartWindow::replyAddIntoShopCart(Food food){
//    qDebug()<<food.name<<" "<<food.price<<" ";  //测试
    /**
      * @leiteorz
      * 绘制ListItem
      */
    QListWidgetItem *item = new QListWidgetItem(this);
    item->setSizeHint(QSize(400,89));
    ShopCartItem *shopCartItem = new ShopCartItem();
    shopCartItem->init(food.name,food.price);
    this->setItemWidget(item,shopCartItem);
    /**
      * @leiteorz
      * 计算总价
      */
    total_price += food.price;
    ui->price_label->setText(QString::number(total_price));
    /**
      * @leiteorz
      * 加入账单
      */
    bill += food.name;
    bill += "/";    //分隔符
    bill += QString::number(food.price);
    bill += "/";    //分隔符
}

//显示购物车界面
void ShopCartWindow::replyShowShopCartWindow(){
    this->show();
}

//结账按钮
void ShopCartWindow::on_settle_btn_clicked(){
    /**
      * @leiteorz
      * 获取账单信息
      */
    FoodOrder food_order;   //账单
    FoodOrderTable food_order_table;    //操作数据库
    //当前用户,即下单人
    CurrentUser current_user;
    User user = current_user.queryCurrentUser();
    food_order.user_name = user.name;
    food_order.user_phone = user.phone_num;
    food_order.u_id = user.account;
    //账单内容
    food_order.bill = bill;
    //账单价钱
    food_order.price = total_price;
    //添加进数据库中
    food_order_table.insertFoodOrder(food_order);
    /**
      * @leiteorz
      * 清空复原
      */
    bill = "";
    total_price = 0;
    ui->price_label->setText(QString::number(total_price));
    this->clear();
    /**
      * @leiteorz
      * 刷新订单列表ui
      */
    emit dataChange();
}
